home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / SOURCE / LIBPNG / PNG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-16  |  9.3 KB  |  319 lines  |  [TEXT/CWIE]

  1.  
  2. /* png.c - location for general purpose png functions
  3.  
  4.    libpng 1.0 beta 4 - version 0.90
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    January 10, 1997
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #define PNG_NO_EXTERN
  12. #include "png.h"
  13.  
  14. /* version information for c files.  This better match the version
  15.    string defined in png.h */
  16. char png_libpng_ver[] = "0.90";
  17.  
  18. /* place to hold the signiture string for a png file. */
  19. png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  20.  
  21. /* constant strings for known chunk types.  If you need to add a chunk,
  22.    add a string holding the name here.  If you want to make the code
  23.    portable to EBCDIC machines, use ASCII numbers, not characters. */
  24. png_byte FARDATA png_IHDR[4] = { 73,  72,  68,  82};
  25. png_byte FARDATA png_IDAT[4] = { 73,  68,  65,  84};
  26. png_byte FARDATA png_IEND[4] = { 73,  69,  78,  68};
  27. png_byte FARDATA png_PLTE[4] = { 80,  76,  84,  69};
  28. png_byte FARDATA png_gAMA[4] = {103,  65,  77,  65};
  29. png_byte FARDATA png_sBIT[4] = {115,  66,  73,  84};
  30. png_byte FARDATA png_cHRM[4] = { 99,  72,  82,  77};
  31. png_byte FARDATA png_tRNS[4] = {116,  82,  78,  83};
  32. png_byte FARDATA png_bKGD[4] = { 98,  75,  71,  68};
  33. png_byte FARDATA png_hIST[4] = {104,  73,  83,  84};
  34. png_byte FARDATA png_tEXt[4] = {116,  69,  88, 116};
  35. png_byte FARDATA png_zTXt[4] = {122,  84,  88, 116};
  36. png_byte FARDATA png_pHYs[4] = {112,  72,  89, 115};
  37. png_byte FARDATA png_oFFs[4] = {111,  70,  70, 115};
  38. png_byte FARDATA png_tIME[4] = {116,  73,  77,  69};
  39.  
  40. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  41.  
  42. /* start of interlace block */
  43. int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  44.  
  45. /* offset to next interlace block */
  46. int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  47.  
  48. /* start of interlace block in the y direction */
  49. int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  50.  
  51. /* offset to next interlace block in the y direction */
  52. int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  53.  
  54. /* width of interlace block */
  55. /* this is not currently used - if you need it, uncomment it here and
  56.    in png.h
  57. int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
  58. */
  59.  
  60. /* height of interlace block */
  61. /* this is not currently used - if you need it, uncomment it here and
  62.    in png.h
  63. int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  64. */
  65.  
  66. /* mask to determine which pixels are valid in a pass */
  67. int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  68.  
  69. /* mask to determine which pixels to overwrite while displaying */
  70. int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  71.  
  72.  
  73. /* Tells libpng that we have already handled the first "num_bytes" bytes
  74.  * of the PNG file signature.  If the PNG data is embedded into another
  75.  * stream we can set num_bytes = 8 so that libpng will not attempt to read
  76.  * or write any of the magic bytes before it starts on the IHDR.
  77.  */
  78. void
  79. png_set_sig_bytes(png_structp png_ptr, int num_bytes)
  80. {
  81.    if (num_bytes > 8)
  82.       png_error(png_ptr, "Too many bytes for PNG signature.");
  83.  
  84.    png_ptr->sig_bytes = num_bytes < 0 ? 0 : num_bytes;
  85. }
  86.  
  87. /* Checks whether the supplied bytes match the PNG signature.  We allow
  88.  * checking less than the full 8-byte signature so that those apps that
  89.  * already read the first few bytes of a file to determine the file type
  90.  * can simply check the remaining bytes for extra assurance.  Returns
  91.  * an integer less than, equal to, or greater than zero if sig is found,
  92.  * respectively, to be less than, to match, or be greater than the correct
  93.  * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
  94.  */
  95. int
  96. png_sig_cmp(png_bytep sig, int start, int num_to_check)
  97. {
  98.    if (num_to_check > 8)
  99.       num_to_check = 8;
  100.    else if (num_to_check < 1)
  101.       return 0;
  102.  
  103.    if (start > 7 || start < 0)
  104.       return 0;
  105.  
  106.    if (start + num_to_check > 8)
  107.       num_to_check = 8 - start;
  108.  
  109.    return (png_memcmp(sig, &png_sig[start], (unsigned int)num_to_check));
  110. }
  111.  
  112. /* (Obsolete) function to check signature bytes.  It does not allow one
  113.    to check a partial signature.  This function will be removed in the
  114.    future - use png_sig_cmp(). */
  115. int
  116. png_check_sig(png_bytep sig, int num)
  117. {
  118.   return !png_sig_cmp(sig, 0, num);
  119. }
  120.  
  121. /* Function to allocate memory for zlib. */
  122. voidpf
  123. png_zalloc(voidpf png_ptr, uInt items, uInt size)
  124. {
  125.    png_voidp ptr;
  126.    png_uint_32 num_bytes;
  127.  
  128.    ptr = png_malloc((png_structp)png_ptr,
  129.       (png_uint_32)items * (png_uint_32)size);
  130.    num_bytes = (png_uint_32)items * (png_uint_32)size;
  131.    if (num_bytes > (png_uint_32)0x7fff)
  132.    {
  133.       png_memset(ptr, 0, (png_size_t)0x8000L);
  134.       png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  135.          (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  136.    }
  137.    else
  138.    {
  139.       png_memset(ptr, 0, (png_size_t)num_bytes);
  140.    }
  141.    return (voidpf)(ptr);
  142. }
  143.  
  144. /* function to free memory for zlib */
  145. void
  146. png_zfree(voidpf png_ptr, voidpf ptr)
  147. {
  148.    png_free((png_structp)png_ptr, (png_voidp)ptr);
  149. }
  150.  
  151. /* Reset the CRC variable to 32 bits of 1's.  Care must be taken
  152.    in case CRC is > 32 bits to leave the top bits 0. */
  153. void
  154. png_reset_crc(png_structp png_ptr)
  155. {
  156.    /* set crc to all 1's */
  157. #ifdef PNG_USE_OWN_CRC
  158.    png_ptr->crc = 0xffffffffL;
  159. #else
  160.    png_ptr->crc = crc32(0, Z_NULL, 0);
  161. #endif
  162. }
  163.  
  164. #ifdef PNG_USE_OWN_CRC
  165. /* Table of CRC's of all 8-bit messages.  If you wish to png_malloc this
  166.    table, turn this into a pointer, and png_malloc() it in make_crc_table().
  167.    You may then want to hook it into png_struct and free it with the
  168.    destroy functions.  Another alternative is to pre-fill the table.  */
  169. static png_uint_32 crc_table[256];
  170.  
  171. /* Flag: has the table been computed? Initially false. */
  172. static int crc_table_computed = 0;
  173.  
  174. /* make the table for a fast crc */
  175. static void
  176. make_crc_table(void)
  177. {
  178.   png_uint_32 c;
  179.   int n, k;
  180.  
  181.   for (n = 0; n < 256; n++)
  182.   {
  183.    c = (png_uint_32)n;
  184.    for (k = 0; k < 8; k++)
  185.      c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
  186.    crc_table[n] = c;
  187.   }
  188.   crc_table_computed = 1;
  189. }
  190.  
  191. /* Update a running CRC with the bytes buf[0..len-1]--the crc should be
  192.    initialized to all 1's, and the transmitted value is the 1's complement
  193.    of the final running CRC. */
  194. static png_uint_32
  195. update_crc(png_uint_32 crc, png_bytep buf, png_uint_32 len)
  196. {
  197.   png_uint_32 c;
  198.   png_bytep p;
  199.   png_uint_32 n;
  200.  
  201.   c = crc;
  202.   p = buf;
  203.   n = len;
  204.  
  205.   if (!crc_table_computed)
  206.   {
  207.    make_crc_table();
  208.   }
  209.  
  210.   if (n > 0) do
  211.   {
  212.    c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
  213.   } while (--n);
  214.  
  215.   return c;
  216. }
  217. #endif /* PNG_USE_OWN_CRC */
  218.  
  219. /* Calculate the crc over a section of data.  Note that while we
  220.    are passing in a 32 bit value for length, on 16 bit machines, you
  221.    would need to use huge pointers to access all that data.  If you
  222.    need this, put huge here and above. */
  223. void
  224. png_calculate_crc(png_structp png_ptr, png_bytep ptr,
  225.    png_uint_32 length)
  226. {
  227. #ifdef PNG_USE_OWN_CRC
  228.    png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
  229. #else
  230.    png_ptr->crc = crc32(png_ptr->crc, ptr, length);
  231. #endif
  232. }
  233.  
  234. /* Allocate the memory for an info_struct for the application.  We don't
  235.    really need the png_ptr, but it could potentially be useful in the
  236.    future.  This should be used in favour of malloc(sizeof(png_info))
  237.    and png_info_init() so that applications that want to use a shared
  238.    libpng don't have to be recompiled if png_info changes size. */
  239. png_infop
  240. png_create_info_struct(png_structp png_ptr)
  241. {
  242.    png_infop info_ptr;
  243.  
  244.    if ((info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO)) != NULL)
  245.    {
  246.       png_info_init(info_ptr);
  247.    }
  248.  
  249.    return info_ptr;
  250. }
  251.  
  252. /* This function frees the memory associated with a single info struct.
  253.    Normally, one would use either png_destroy_read_struct() or 
  254.    png_destroy_write_struct() to free an info struct, but this may be
  255.    useful for some applications. */
  256. void
  257. png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
  258. {
  259.    png_infop info_ptr = NULL;
  260.  
  261.    if (info_ptr_ptr)
  262.       info_ptr = *info_ptr_ptr;
  263.  
  264.    if (info_ptr)
  265.    {
  266.       png_info_destroy(png_ptr, info_ptr);
  267.  
  268.       png_destroy_struct((png_voidp)info_ptr);
  269.       *info_ptr_ptr = (png_infop)NULL;
  270.    }
  271. }
  272.  
  273. /* Initialize the info structure.  This is now an internal function (0.89)
  274.    and applications using it are urged to use png_create_info_struct()
  275.    instead. */
  276. void
  277. png_info_init(png_infop info_ptr)
  278. {
  279.    /* set everything to 0 */
  280.    png_memset(info_ptr, 0, sizeof (png_info));
  281. }
  282.  
  283. /* This is an internal routine to free any memory that the info struct is
  284.    pointing to before re-using it or freeing the struct itself. */
  285. void
  286. png_info_destroy(png_structp png_ptr, png_infop info_ptr)
  287. {
  288. #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
  289.    int i;
  290.  
  291.    for (i = 0; i < info_ptr->num_text; i++)
  292.    {
  293.       png_free(png_ptr, info_ptr->text[i].key);
  294.    }
  295.  
  296.    png_free(png_ptr, info_ptr->text);
  297. #endif
  298.  
  299.    png_info_init(info_ptr);
  300. }
  301.  
  302. /* This function returns a pointer to the io_ptr associated with the user
  303.    functions.  The application should free any memory associated with this
  304.    pointer before png_write_destroy() or png_read_destroy() are called. */
  305. png_voidp
  306. png_get_io_ptr(png_structp png_ptr)
  307. {
  308.    return png_ptr->io_ptr;
  309. }
  310.  
  311. /* Initialize the default input/output functions for the png file.  If you
  312.    change the read, or write routines, you can call either png_set_read_fn()
  313.    or png_set_write_fn() instead of png_init_io(). */
  314. void
  315. png_init_io(png_structp png_ptr, FILE *fp)
  316. {
  317.    png_ptr->io_ptr = (png_voidp)fp;
  318. }
  319.